package jude.java.money.service;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;

import jude.java.money.beans.global.BaseBean;
import jude.java.money.dao.GlobalDao;

import org.hibernate.type.Type;

/** 
 * service 基类, 实现基本(CRUD)操作
 * jude.java.framework.dao.BaseHibernateDao
 *  */
public abstract class BaseService {

	protected GlobalDao dao;
	
	public <T extends BaseBean> void insert(T o) {
		dao.insert(o);
	}
	
	public <T extends BaseBean> void update(T o) {
		dao.update(o);
	}
	
	public <T extends BaseBean> int update(Class<T> clz, String hql, Object[] values) {
		return dao.update(clz, hql, values);
	}
	
	public <T extends BaseBean> void save(T o) {
		dao.insertOrUpdate(o);
	}
	
	public <T extends BaseBean> void saveAll(Collection<T> o) {
		dao.insertOrUpdateAll(o);
	}
	
	public <T extends BaseBean> void delete(T o) {
		dao.delete(o);
	}
	
	public <T extends BaseBean>  void delete(Class<T> clz, Serializable id) {
		dao.delete(clz, id);
	}
	
	public <T extends BaseBean> T get(Class<T> clz, Serializable id) {
		return dao.get(clz, id);
	}
	
	public <T extends BaseBean> T get(Class<T> clazz, String key, Object value) {
		return dao.get(clazz, key, value);
	}
	
	public <T extends BaseBean>  List<T> listAll(Class<T> clz) {
		return dao.listAll(clz);
	}
	
	public <T extends BaseBean> List<T> listAll(Class<T> clazz, String key, Object value) {
		return dao.query(clazz, key, value);
	}
	
	@Deprecated
	public <T extends BaseBean> List<T> query(Class<T> clazz, String search, Object... values) {
		return dao.query(clazz, search, values);
	}
	
	///分页查询, 所有数据
	public <T extends BaseBean> int countAll(Class<T> clazz) {
		return dao.count(clazz, null, null, null);
	}
	
	public <T extends BaseBean> List<T> listAll(Class<T> clazz, int pageSize, int pageIndex) {
		return dao.query(clazz, null, pageSize, pageIndex, null, null);
	}
	
	///分页查询, 不带参数
	public <T extends BaseBean> int count(Class<T> clazz, String search) {
		return dao.count(clazz, search, null, null);
	}
	
	public <T extends BaseBean> List<T> query(Class<T> clazz, String search, int pageSize, int pageIndex) {
		return dao.query(clazz, search, pageSize, pageIndex, null, null);
	}
	
	///分页查询, 带一个参数
	/**
	 * @param value not null
	 * */
	public <T extends BaseBean> int count(Class<T> clazz, String search, Object value) {
		return dao.count(clazz, search, new Object[] {value}, null);
	}
	
	/**
	 * @param value not null
	 * */
	public <T extends BaseBean> List<T> query(Class<T> clazz, String search, int pageSize, int pageIndex, Object value) {
		return dao.query(clazz, search, pageSize, pageIndex, new Object[] {value}, null);
	}
	
	///分页查询, 带多个参数
	public <T extends BaseBean> int count(Class<T> clazz, String search, Object[] values, Type[] types) {
		return dao.count(clazz, search, values, types);
	}
	
	public <T extends BaseBean> List<T> query(Class<T> clazz, String search, int pageSize, int pageIndex, Object[] values, Type[] types) {
		return dao.query(clazz, search, pageSize, pageIndex, values, types);
	}
	
	public List<?> query(String hql){
		return dao.query(hql);
	}
	
	public Object get(String hql) {
		return dao.get(hql);
	}
	
	public int update(String hql) {
		return dao.update(hql);
	}
	
	/** @return List&lt;Object&gt; */
	public Object executeSql(final String sql) {
		return dao.executeSql(sql);
	}
	
	/** 仅一结果的查询, 更新(insert/update/delete/select count(*)/...) */
	public Object executeUniqueResultSql(final String sql) {
		return dao.executeUniqueResultSql(sql);
	}

	
	public void setGlobalDao(GlobalDao dao) {
		this.dao = dao;
	}
}
